<?php
namespace app\admin\controller;

use app\common\entity\User;
use app\common\shop\UserAgentLevelModel;
use app\common\shop\UserAreaLevelModel;
use app\common\shop\UserHouseLevelModel;
use app\common\shop\UserTeamLevelModel;
use think\Db;
use think\Request;
use think\Controller;
class Userlevel extends Admin {
    /**
     * [团长设置]
     * @param
     * @return
     */
    public function team( Request $request ){
        $team = new UserTeamLevelModel();
        $list = $team->paginate(15, false,[
            'query'=>$request->param()  ?   $request->param():[]
        ]);

        return $this->render('team', [
            'list' => $list,
        ]);
    }

    /**
     * [团长修改]
     * @param
     * @return
     */
    public function teamsave( Request $request ){
        $id = input('id');
        if(empty($id)) return json(['code'=>1,'message'=>'操作错误']);
        $info = UserTeamLevelModel::where('id',$id)->find();
        if (!$info) {
            return json(['code'=>1,'message'=>'操作错误']);
        }
        $res = UserTeamLevelModel::where('id',$id)->update(input());
        if (!$res) {
            return ['code' => 1, 'message' => '修改失败'];
        }
        return ['code' => 0, 'message' => '修改成功'];
    }
    /**
     * [代理商设置]
     * @param
     * @return
     */
    public function agent( Request $request ){
        $team = new UserAgentLevelModel();
        
        $list = $team->paginate(15, false, [
            'query'=>$request->param()  ? $request->param():[]
        ]);

        $t = UserTeamLevelModel::order('id asc')->select()->toArray();
        return $this->render('agent', [
            'list' => $list,
            'team' => $t,
        ]);
    }

    /**
     * [代理商修改]
     * @param
     * @return
     */
    public function agentsave( Request $request ){
        $id = input('id');
        if(empty($id)) return json(['code'=>1,'message'=>'操作错误']);
        $info = UserAgentLevelModel::where('id',$id)->find();
        if (!$info) {
            return json(['code'=>1,'message'=>'操作错误']);
        }
        $res = UserAgentLevelModel::where('id',$id)->update(input());
        if (!$res) {
            return ['code' => 1, 'message' => '修改失败'];
        }
        return ['code' => 0, 'message' => '修改成功'];
    }

    /**
     * [代理商设置]
     * @param
     * @return
     */
    public function house( Request $request ){
        $team = new UserHouseLevelModel();

        $list = $team->paginate(15, false, [
            'query'=>$request->param()  ? $request->param():[]
        ]);

        $t = UserTeamLevelModel::order('id asc')->select()->toArray();
        return $this->render('house', [
            'list' => $list,
            'team' => $t,
        ]);
    }

    /**
     * [代理商修改]
     * @param
     * @return
     */
    public function housesave( Request $request ){
        $id = input('id');
        if(empty($id)) return json(['code'=>1,'message'=>'操作错误']);
        $info = UserHouseLevelModel::where('id',$id)->find();
        if (!$info) {
            return json(['code'=>1,'message'=>'操作错误']);
        }
        $res = UserHouseLevelModel::where('id',$id)->update(input());
        if (!$res) {
            return ['code' => 1, 'message' => '修改失败'];
        }
        return ['code' => 0, 'message' => '修改成功'];
    }

    /**
     * [代理商设置]
     * @param
     * @return
     */
    public function agency( Request $request ){
        $team = new UserAreaLevelModel();
//        new UserAreaLevelModel()
        $list = $team->paginate(15, false, [
            'query'=>$request->param()  ? $request->param():[]
        ]);

        $t = UserTeamLevelModel::order('id asc')->select()->toArray();
        return $this->render('agency', [
            'list' => $list,
            'team' => $t,
        ]);
    }

    /**
     * [代理商修改]
     * @param
     * @return
     */
    public function agencysave( Request $request ){
        $id = input('id');
        if(empty($id)) return json(['code'=>1,'message'=>'操作错误']);
        $info = UserAreaLevelModel::where('id',$id)->find();
        if (!$info) {
            return json(['code'=>1,'message'=>'操作错误']);
        }
        $res = UserAreaLevelModel::where('id',$id)->update(input());
        if (!$res) {
            return ['code' => 1, 'message' => '修改失败'];
        }
        return ['code' => 0, 'message' => '修改成功'];
    }
}